/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.item;

import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.gameobjects.*;
import com.aionengine.gameserver.model.house.House;
import com.aionengine.gameserver.model.templates.housing.*;
import com.aionengine.gameserver.model.templates.item.ItemTemplate;
import com.aionengine.gameserver.model.templates.item.actions.SummonHouseObjectAction;
import com.aionengine.gameserver.utils.idfactory.IDFactory;
import org.apache.commons.lang.IncompleteArgumentException;
import org.joda.time.DateTime;

/**
 * @author Rolandas
 */
public final class HouseObjectFactory {

    /**
     * For loading data from DB
     */
    public static HouseObject<?> createNew(House house, int objectId, int objectTemplateId) {
        PlaceableHouseObject template = DataManager.HOUSING_OBJECT_DATA.getTemplateById(objectTemplateId);
        if (template instanceof HousingChair)
            return new ChairObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingJukeBox)
            return new JukeBoxObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingMoveableItem)
            return new MoveableObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingNpc)
            return new NpcObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingPicture)
            return new PictureObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingPostbox)
            return new PostboxObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingStorage)
            return new StorageObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingUseableItem)
            return new UseableItemObject(house, objectId, template.getTemplateId());
        else if (template instanceof HousingEmblem)
            return new EmblemObject(house, objectId, template.getTemplateId());
        return new PassiveObject(house, objectId, template.getTemplateId());
    }

    /**
     * For transferring item from inventory to house registry
     */
    public static HouseObject<?> createNew(House house, ItemTemplate itemTemplate) {
        if (itemTemplate.getActions() == null)
            throw new IncompleteArgumentException("template actions null");
        SummonHouseObjectAction action = itemTemplate.getActions().getHouseObjectAction();
        if (action == null)
            throw new IncompleteArgumentException("template actions miss SummonHouseObjectAction");

        int objectTemplateId = action.getTemplateId();
        HouseObject<?> obj = createNew(house, IDFactory.getInstance().nextId(), objectTemplateId);
        if (obj.getObjectTemplate().getUseDays() > 0) {
            int expireEnd = (int) (DateTime.now().plusDays(obj.getObjectTemplate().getUseDays()).getMillis() / 1000);
            obj.setExpireTime(expireEnd);
        }
        return obj;
    }
}
